/*
 * $Id$
 *
 * Copyright ( c ) 2014 XuNuo Corporation. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of XuNuo
 * Corporation ("Confidential Information").  You shall not disclose such
 * Confidential Information and shall use it only in accordance with the terms
 * of the license agreement you entered into with XuNuo Corporation or a XuNuo
 * authorized reseller (the "License Agreement"). XuNuo may make changes to the
 * Confidential Information from time to time. Such Confidential Information may
 * contain errors.
 *
 * EXCEPT AS EXPLICITLY SET FORTH IN THE LICENSE AGREEMENT, XuNuo DISCLAIMS ALL
 * WARRANTIES, COVENANTS, REPRESENTATIONS, INDEMNITIES, AND GUARANTEES WITH
 * RESPECT TO SOFTWARE AND DOCUMENTATION, WHETHER EXPRESS OR IMPLIED, WRITTEN OR
 * ORAL, STATUTORY OR OTHERWISE INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A
 * PARTICULAR PURPOSE. XuNuo DOES NOT WARRANT THAT END USER'S USE OF THE
 * SOFTWARE WILL BE UNINTERRUPTED, ERROR FREE OR SECURE.
 *
 * XuNuo SHALL NOT BE LIABLE TO END USER, OR ANY OTHER PERSON, CORPORATION OR
 * ENTITY FOR INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL
 * DAMAGES, OR DAMAGES FOR LOSS OF PROFITS, REVENUE, DATA OR USE, WHETHER IN AN
 * ACTION IN CONTRACT, TORT OR OTHERWISE, EVEN IF XuNuo HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES. XuNuo' TOTAL LIABILITY TO END USER SHALL NOT
 * EXCEED THE AMOUNTS PAID FOR THE XuNuo SOFTWARE BY END USER DURING THE PRIOR
 * TWELVE (12) MONTHS FROM THE DATE IN WHICH THE CLAIM AROSE.  BECAUSE SOME
 * STATES OR JURISDICTIONS DO NOT ALLOW LIMITATION OR EXCLUSION OF CONSEQUENTIAL
 * OR INCIDENTAL DAMAGES, THE ABOVE LIMITATION MAY NOT APPLY TO END USER.
 *
 * Copyright version 2.0
 */
package com.th.supcom.pds.impl.service.internal.chargedpresc;

import java.util.Map;

import net.carefx.framework.exception.BusinessException;

import org.apache.commons.chain.impl.ChainBase;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 收方收费任务链启动类
 * 
 * @author wangweiquan
 */
public final class ChargedPrescriptionChain
{

    private ChainBase m_chainBase;

    /**
     * 执行收方收费业务
     * 
     * @throws IllegalArgumentException 初始化收方收费任务链失败
     * @throws BusinessException 执行收方收费时出现了业务异常，
     * @return 收方收费处理上下文
     */
    public final Map<String,Object> exeucte (ChargedPrescriptionContext context)
    {
        if (s_logger.isInfoEnabled ())
        {
            s_logger.info ("启动收方收费后台业务处理链，参数信息：{}", context);
        }
        if (null == context)
        {
            throw new IllegalArgumentException ("收方收费上下文不允许为空");
        }
        if (null == m_chainBase)
        {
            throw new IllegalArgumentException ("没有找到收方收费处方链，请确认配置是正确的");
        }
        try
        {
            m_chainBase.execute (context);
            if (s_logger.isInfoEnabled ())
            {
                s_logger.info ("收方收费后台业务处理链执行结束,当前上下文数据：{}", context);
            }
        }
        catch (BusinessException ex)
        {
            throw ex;
        }
        catch (Exception ex)
        {
            if (s_logger.isErrorEnabled ())
            {
                s_logger.error ("收方收费链执行失败\r\n上下文信息：{}", new Object[]
                { context, ex });
            }
            throw new BusinessException ("后台收方收费执行失败，请检查相关配置", ex);
        }
        return context.getMap ();
    }

    /**
     * 通过Spring注入收方收费处理链
     * 
     * @param chainBase the chainBase to set.
     */
    public void setChainBase (ChainBase chainBase)
    {
        m_chainBase = chainBase;
    }

    /** 日志记录器. */
    private static Logger s_logger = LoggerFactory.getLogger (ChargedPrescriptionChain.class);

    /** 公司版权信息. */
    public static final String copyright = "Copyright 2014, TH Supcom Corporation";

    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";

    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";

    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";

}
